<?php 
class cls_data_address extends cls_data_base
{
	function check_if_related_data($customerid, $addressid)
	{
		$table_name = '`address`';
		
		$sql = "select * from {$table_name} where CustomerID = {$customerid} and ID = {$addressid}";
		$result = $this->db->query($sql);
		$rows = $this->db->num_rows($result);
		
		if(!$rows)
		{
			return false;
		}
		elseif ($rows != 1)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function get_all_address($customerid)
	{
		$table_name = '`address`';
	
		$sql = "select * from {$table_name} where CustomerID = {$customerid}";
		$result = $this->db->getAll($sql);
	
		return $result;
	}
	
	function get_default_addressid($customerid)
	{
		$table_name = '`customer`';
		
		$sql = "select DefaultAddressID from {$table_name} where ID = {$customerid}";
		$result = $this->db->getRow($sql);
		
		return $result;
	}
	
	function get_default_address($customerid)
	{
		$customer_table_name = '`customer`';
		$address_table_name = '`address`';
	
		//$sql = "select DefaultAddressID from {$table_name} where ID = {$customerid}";
		$sql = "select {$customer_table_name}.DefaultAddressID, {$address_table_name}.Receiver, {$address_table_name}.Phone, {$address_table_name}.Address, {$address_table_name}.Postal from {$address_table_name}, {$customer_table_name} where {$customer_table_name}.ID = {$customerid} and {$customer_table_name}.DefaultAddressID = {$address_table_name}.ID";
		$result = $this->db->getRow($sql);
	
		return $result;
	}
	
	function add_address($customerid, $postal, $receiver, $phone, $address, $description)
	{
		$table_name = '`address`';
	
		$input = array(
				"CustomerID" => $customerid,
				"Postal" => $postal,
				"Receiver" => $receiver,
				"Phone" => $phone,
				"Address" => $address,
				"Description" => $description);
		$result = $this->db->insert($table_name, $input);
	
		return $result;
	}
	
	function delete_address($addressid)
	{
		$table_name = '`address`';
	
		$result = $this->db->delete($table_name, "ID = {$addressid}");
		return $result;
	}
	
	function update_address($addressid, $postal, $receiver, $phone, $address, $description)
	{
		$table_name = '`address`';
	
		$input = array(
				"ID" => $addressid,
				"Postal" => $postal,
				"Receiver" => $receiver,
				"Phone" => $phone,
				"Address" => $address,
				"Description" => $description);
		$result = $this->db->update($table_name, $input, "ID = {$addressid}", 1);
		return $result;
	}
	
	function set_default_address($customerid, $addressid)
	{
		$table_name = '`customer`';
	
		$input = array(
				"DefaultAddressID" => $addressid);
	
		$result = $this->db->update($table_name, $input, "ID = {$customerid}", 1);
		return $result;
	}
}
?>